"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : GUC
Case Name   : proc_outparam_override打开后，函数参数中带有多个refcursor类型的out参数，call方式调用函数
Create At   : 2023/10/10
Owner       : n@ningyali
Description :
    1、建表插入数据
    2、创建函数，带有多个refcursor类型的out参数
    3、设置proc_outparam_override参数值为proc_outparam_override,调用函数
Expect      :
    1、执行成功
    2、执行成功
    3、设置成功，函数调用成功
History     :
"""

import os
import re
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class GunOtherOption60(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.constant = Constant()
        self.common = Common()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.t_name = 'public.t_guc_other_option_60'
        self.f_name = 'public.f_guc_other_option_60'

    def test_guc(self):
        text = '-----step1:建表插入数据; expect:执行成功-----'
        self.log.info(text)
        sql = f"drop table if exists {self.t_name};" \
            f"create table {self.t_name}(coll int,col2 text);" \
            f"insert into {self.t_name} values (1, 'test');"
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        expect = 'DROP TABLE.*CREATE TABLE.*INSERT 0 1'
        regex_res = re.search(expect, res, re.S)
        self.assertIsNotNone(regex_res, f'执行失败:{text}')

        text = '-----step2:创建函数，带有多个refcursor类型的out参数; expect:执行成功-----'
        self.log.info(text)
        sql = f'create or replace function {self.f_name}(out rl refcursor,' \
            f'out r2 refcursor)\n' \
            f'returns refcursor as\n' \
            f'\\$\\$\n' \
            f'begin\n' \
            f'  open rl for select coll from {self.t_name};\n' \
            f'  open r2 for select col2 from {self.t_name};\n' \
            f'return r2;\n' \
            f'end;\n' \
            f'\\$\\$\n' \
            f'LANGUAGE \'plpgsql\';'
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.assertIn('CREATE FUNCTION', res, f'执行失败:{text}')

        text = '-----step3:设置proc_outparam_override参数值为' \
               'proc_outparam_override,调用函数; ' \
               'expect:设置成功，函数调用成功-----'
        self.log.info(text)
        sql = f"set behavior_compat_options = 'proc_outparam_override';\n" \
            f"call {self.f_name}('','');"
        res = self.pri_sh.execut_db_sql(sql)
        _, cols = self.common.parse_sql_query_res(res)
        expect = [['<unnamed portal 2>'], ['<unnamed portal 1>'],
                  ['<unnamed portal 2>']]
        self.assertEqual(cols, expect, f'执行失败:{text}')

    def tearDown(self):
        text = '-----step4:清理环境; expect:执行成功-----'
        self.log.info(text)
        sql = f"drop function if exists {self.f_name};" \
            f"drop table if exists {self.t_name};"
        res = self.pri_sh.execut_db_sql(sql)
        self.log.info(res)
        self.log.info(f"-----{os.path.basename(__file__)} stop-----")
